<?php
/*
Plugin Name: BPDEV-AUTOSUGGEST
Plugin URI: http://bp-dev.org/projects/#autosuggest
Description: This plugin adds Auto suggest support to your BuddyPress searchbox!
Version: 0.3
Author: Nicola Greco(BP), Elie El Khoury(WP)
Author URI: http://nicolagreco.com
Original Author URI: http://www.ekhoury.com
*/

define( 'AUTOSUGGEST_DIR', get_option('siteurl') . '/' . MUPLUGINDIR . '/bpdev-autosuggest' );
define( 'BPDEV_AUTOSUGGEST_NAME', 'BPDEV-AUTOSUGGEST' );
define( 'BPDEV_AUTOSUGGEST_VERSION', '0.3' );

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );

add_action( 'wp', 'bpdev_autosuggest_setup_globals', 1 );
add_action( '_admin_menu', 'bpdev_autosuggest_setup_globals', 1 );

if ( !get_option( 'bpdev-autosuggest-status' ) ) add_option( 'bpdev-autosuggest-status' , 'off' );
if ( get_option( 'bpdev-autosuggest-status' ) == "on" ) {

	add_action( 'bpdev_theme_extra_setup_globals' , 'bpdev_autosuggest_css' );
	add_action( 'wp_head' , 'bpdev_autosuggest_javascript' );
	add_action( 'wp_footer' , 'bpdev_autosuggest_footer_code' );

}

add_action( 'bpdev_admin_plugins_screen', 'bpdev_autosuggest_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save', 'bpdev_autosuggest_admin_menu_save', 3 );

function bpdev_autosuggest_setup_globals() {

	global $bp;
	$bp->bpdev->autosuggest->slug ='autosuggest';
	$bp->bpdev->autosuggest->image_base = site_url() . '/wp-content/mu-plugins/bpdev-autosuggest/images'; //Images
	$bp->bpdev->autosuggest->css = site_url() . '/' . bpdev_component_dir( $bp->bpdev->autosuggest->slug ) . 'css/';
	$bp->bpdev->autosuggest->js = site_url() . '/' . bpdev_component_dir( $bp->bpdev->autosuggest->slug ) . 'js/';
	$bp->bpdev->autosuggest->dir = site_url() . '/' . bpdev_component_dir( $bp->bpdev->autosuggest->slug );

}

function bpdev_autosuggest_css() {

	bpdev_theme_register_style(
		'bpdev-autosuggest', 
		'BPDEV Autosuggest', 
		array( 'BPDEV_Theme_CSS' , 'bpdev_autosuggest' ),
		'on',
		'off'
	);

}

function bpdev_autosuggest_load_users_by_word( $bpdev_as_keys ) {

	global $bp;
	return  BP_Core_User::search_users( $bpdev_as_keys );

}

function bpdev_autosuggest_javascript() {

	global $bp;
	?>
<!-- WP AutoSuggest Code Starts -->
<script src="<?php echo $bp->bpdev->autosuggest->js; ?>wp.autosuggest.js?ver=1" type="text/javascript"></script>
<!-- WP AutoSuggest Code Ends -->
	<?php

}

function bpdev_autosuggest_footer_code() {

	global $bp;
	?>
<script type="text/javascript">
var autosuggest_options = {
	script: "<?php echo $bp->bpdev->autosuggest->dir; ?>autosuggest.php?bpdev_as_action=query&",
	varname: "bpdev_as_keys",
	shownoresults:true,
	noresults:"<?php echo get_bpdev_as_option('bpdev_as_noresults','Not found in titles, press enter to search content!'); ?>",
	timeout:15000,
	callback:autosuggestSelected,
	maxresults: <?php echo get_bpdev_as_option('bpdev_as_maxresults','10'); ?>
};
var as = new AutoSuggest('bpdev-terms', autosuggest_options);
function autosuggestSelected(entry) {
    document.location = entry['id'];
}
</script>
	<?php

}

function get_bpdev_as_option($key, $default) {

	$value = stripslashes( get_option( $key ) );

	if ( $value == '' ) {

		$value = $default;

	}
	
	return $value;

}

function bpdev_autosuggest_admin_menu_save() {

	do_action( 'bpdev_admin_save_component', 'bpdev-autosuggest-status' );
	do_action( 'bpdev_admin_save_component', 'bpdev_as_maxresults' );
	do_action( 'bpdev_admin_save_component', 'bpdev_as_noresults' );

}

function bpdev_autosuggest_admin_menu_screen() {

	?>

	<h3><?php echo BPDEV_AUTOSUGGEST_NAME; ?></h3>
	<table class="form-table">
		<tr valign="top">
			<th scope="row">Plugin Version</th>
			<td>
				<?php echo BPDEV_AUTOSUGGEST_VERSION; ?>
			</td>
		</tr>
		<tr>
			<th scope="row">Plugin Status</th>
			<td>
				<select id="bpdev-autosuggest-status" name="bpdev-autosuggest-status">
					<option value="off"  <?php if ( get_option( 'bpdev-autosuggest-status' ) == "off" || !get_option( 'bpdev-autosuggest-status' ) ) echo 'selected=""'; ?>>Off</option>
					<option value="on" <?php if ( get_option('bpdev-autosuggest-status') == "on") echo 'selected=""'; ?>>On</option>
				</select>
			</td>
		</tr>
		<tr valign="top">

			<th scope="row">Max Results</th>
			<td>
				<input type="text" value="<?php echo get_bpdev_as_option('bpdev_as_maxresults','10'); ?>" id="bpdev_as_maxresults" name="bpdev_as_maxresults"/><br/>
					Maximum number of suggested results (10 by default).
			</td>

		</tr>
		<tr valign="top">

			<th scope="row">No Results</th>
			<td>
				<input type="text" value="<?php echo get_bpdev_as_option('bpdev_as_noresults','Not found in titles, press enter to search content!'); ?>" id="bpdev_as_noresults" name="bpdev_as_noresults"/><br/>
					Text to display when input was not found in titles.
			</td>

		</tr>
	</table>

	<?php
}

?>